<template>
  <el-container class="height100 domainHeader">
    <el-header class="obpm-header-t">
      <admin_header :desc="$t('systemCon')"/>
    </el-header>
    <div class="bgColor clean domainContainer allContainerHeight">
      <div class=" whiteBgColor domainMainContent containerHeight" style="position:relative;">
        <div class="systemBtn" v-show="settingFlag || ldapFlag || policyFlag || configFlag">
          <template v-if="!(policyFlag && isOpen)">
            <button_item  :btnItemLabel="$t('save')"  @onBtnItemClick="onSaveBtnClick" />
            <button_item  btnItemType="plain" :btnItemLabel="$t('import')"  @onBtnItemClick="onImportBtnClick" />
          </template>
          <button_item  btnItemType="plain" :btnItemLabel="$t('export')"  @onBtnItemClick="onExportBtnClick" />
          <button_item  btnItemType="plain" :btnItemLabel="$t('refresh')"  @onBtnItemClick="onRefreshBtnClick" />
          <button_item  btnItemType="plain" :btnItemLabel="$t('exit')"  @onBtnItemClick="onBackBtnClick" />
        </div>
        <div class="systemBtn" v-show="systemfileFlag">
          <button_item  :btnItemLabel="$t('new')"  @onBtnItemClick="onCreateBtnClick" />
          <button_item  :btnItemLabel="$t('del')" btnItemType="danger" :btnItemPlain="true" @onBtnItemClick="onDeleteBtnClick" />
        </div>
      <el-tabs class="systemConTabs"  v-model="activeName"  @tab-click="handleClick" ref="tab">
        <el-tab-pane :label="$t('authConfig')" name="setting" class="setting" v-if="(isOpen && isAdmin) ||(isOpen && !isSecret) || !isOpen" >
        </el-tab-pane>
        <el-tab-pane :label="$t('LDAP')" name="LDAP"  v-if="LdapVisible && ((isOpen && isAdmin) ||(isOpen && !isSecret) || !isOpen)" >
        </el-tab-pane>
        <el-tab-pane :label="$t('policy')" name="policy" class="policy"  v-if="(isOpen && isAdmin && !isAdminDomain) ||(isOpen && isSecret) || !isOpen" >
        </el-tab-pane>
        <el-tab-pane :label="$t('manage')" name="manage" class="manage"  v-if="(isOpen && isAdmin) ||(isOpen && !isSecret) || !isOpen" >
        </el-tab-pane>
        <template v-if="!isOpen">
          <el-tab-pane :label="$t('hxImConfig')" name="config" class="config"  v-if="(isOpen && isAdmin) ||(isOpen && !isSecret) || !isOpen" >
          </el-tab-pane>
        </template>
        <el-tab-pane :label="$t('SystemFile')" name="systemfile" class="systemfile"  v-if="(isOpen && isAdmin) ||(isOpen && !isSecret) || !isOpen">
        </el-tab-pane>
      </el-tabs>
    <div  class="systemContent containerContentHeight">
      <setting :detail="authConfig" @changeLdap="changeLdap" v-if="settingFlag"/>
    <LDAP :detail="ldapConfig" v-if="ldapFlag" />
    <policy :detail="loginConfig" v-if="policyFlag" />
    <manage v-if="manageFlag"/>
    <config :detail="hxImConfig" v-if="configFlag"  />
    <systemfile :detail="SystemFile" :key="systemTimer" v-if="systemfileFlag" ref="systemfile"/>
    <fileUpload :visible.sync="fileUploadVisible" v-if="fileUploadVisible" :nowOption="nowOption" @success="getData"/>
     </div>
    </div>
    </div>

  </el-container>
</template>

<script>
import admin_header from "@/components/admin_header.vue";
import setting from "@/components/setting.vue";
import policy from "@/components/policy.vue";
import manage from "@/components/manage.vue";
import config from "@/components/config.vue";
import LDAP from "@/components/LDAP.vue";
import systemfile from "@/components/systemfile.vue";

import fileUpload from "@/components/fileUpload.vue";
import {delMsg} from '../utils/elementui'
export default {
  components: {
    admin_header,
    setting,
    policy,
    manage,
    config,
    fileUpload,
    LDAP,
    systemfile
  },

  created() {
    if(this.$store.state.currentUser.isOpenSecurity) {
      this.isOpen=true;

      if (this.$store.state.currentUser.type == 'admin') {
        this.isAdmin = true;
        if(this.$store.state.currentUser.departments.length<=0 && this.$store.state.currentUser.domains.length<=0) {
          this.isAdminDomain=true;
        }
      }else if(this.$store.state.currentUser.type == 'secret'){
        this.isSecret=true;
        this.activeName='policy';
      }
    }else{
      this.isOpen=false;

    }
    this.getData();
  },
  
  data() {
    return {
      activeName:"setting",
      systemConfig:{},
      authConfig:{},//身份验证
      loginConfig:{},//用户密码
      hxImConfig:{},//环信配置
      ldapConfig:{},//LDAP配置
      SystemFile:{},//系统字段
      fileUploadVisible:false,
      nowOption:"systemImport",
      LdapVisible:false,
      isOpen:false,//是否开启开关
      isAdmin:false,//是否系统管理员
      isAdminDomain:false,//判断所属域，所属部门是否为空
      isSecret:false,//是否安全保密员
      isBtn:true,//是否展示操作按钮
      systemTimer:'',
      settingFlag:true,
      ldapFlag:false,
      policyFlag:false,
      manageFlag:false,
      configFlag:false,
      systemfileFlag:false,
    };
  },
  mounted(){
    // var h = document.documentElement.clientHeight || document.body.clientHeight;
  },
  methods: {
    getData(){
      this.$api.getConfig({
        onSucess:resp =>{
          if(resp.data.errcode==0){
            this.systemConfig = resp.data;
            
            this.authConfig = this.systemConfig.data.authConfig;
            this.loginConfig = this.systemConfig.data.loginConfig;
            if(this.isOpen) {
              this.loginConfig.noticeMethod = this.loginConfig.noticeMethod.replace('1,','')
              this.loginConfig.noticeMethod = this.loginConfig.noticeMethod.replace('1','')
            }
            this.hxImConfig = this.systemConfig.data.hxImConfig;
            this.ldapConfig = this.systemConfig.data.ldapConfig;
            this.SystemFile = this.systemConfig.data.SystemFile;
            if(this.authConfig.ssoAuth=="cn.myapps.security.sso.ADUserSSO" || this.authConfig.ssoAuth=="cn.myapps.security.sso.ADUserSSOJespa"){
              this.LdapVisible=true;
            }else{
              this.LdapVisible=false;
            }
          }else{
            this.$message({
                showClose: true,
                message: resp.data.errmsg,
                type: "error"
              });
          }
        },
        onError:function(){

        }
      })
    },
    
    handleClick(tab){
      if(tab.name=="systemfile" || tab.name == "manage"){
        this.isBtn = false;
        this.systemTimer = new Date().getTime();
      }else{
        this.isBtn = true;
      }
      this.activeName=tab.name
      this.setFlag(tab.name)
    },
    setFlag(name){
      this.settingFlag=false
      this.ldapFlag=false
      this.policyFlag=false
      this.manageFlag=false
      this.configFlag=false
      this.systemfileFlag=false
      switch (name){
        case 'setting': this.settingFlag=true;break;
        case 'LDAP': this.ldapFlag=true;break;
        case 'policy': this.policyFlag=true;break;
        case 'manage'  :this.manageFlag=true;break;
        case 'config': this.configFlag=true;break;
        case 'systemfile': this.systemfileFlag=true;break;
        default:break;
      }
    },
    onBackBtnClick(){
      this.$router.push({ name: 'home'});
    },
    onRefreshBtnClick(){
      this.getData();
      

    },
    onImportBtnClick(){
      this.fileUploadVisible=true;
    },
    async onExportBtnClick(){
      await delMsg(this.$t('msg_export'), this.$t('msg_tip'),this.$t('msg_confirm'),this.$t('cancel'))
      this.$api.exportConfig(()=>{
          this.$message({
            showClose: true,
            message: this.$t("export_success"),
            type: "success"
          });
      })
    },
    onSaveBtnClick(){
      this.$api.saveConfig({data:this.systemConfig},{
        onSucess:resp =>{
          if(resp.data.errcode==0){
            if(this.loginConfig.encryptionMode){
                var currentUser = this.$store.state.currentUser;
                currentUser.encryptionMode = this.loginConfig.encryptionMode;
                this.$store.commit("saveCurrentUser",currentUser);
            }
            this.$message({
              showClose: true,
              message: resp.data.errmsg,
              type: "success"
            });
          }else{
            this.$message({
              showClose: true,
              message: resp.data.errmsg,
              type: "error"
            });
          }
          
          
        },
        onError:function(){
          
        }
      })
    },
    changeLdap(val){
      this.LdapVisible=val;
    },
    onreturnBtn(){
      this.activeName="setting";
    },
    onCreateBtnClick(){
      this.$refs.systemfile.onCreateBtnClick()
    },
    onDeleteBtnClick(){
      this.$refs.systemfile.onDeleteBtnClick()
    }
  }
};
</script>

<style>
</style>